package com.leyou.item.controller;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.TbCategory;
import com.leyou.item.service.TbCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/**
 * 分类管理
 * @author HM
 */
@RestController
public class CategoryController {

    @Autowired
    private TbCategoryService tbCategoryService;
    /**
     * 根据父id 查询分类集合
     * @param parentId
     * @return
     */
    @GetMapping("/category/of/parent")
    public ResponseEntity<List<CategoryDTO>> findCategoryListByPid(@RequestParam(name = "pid") Long parentId){
        List<CategoryDTO> categoryDTOList = tbCategoryService.findCategoryListByPid(parentId);
        return ResponseEntity.ok(categoryDTOList);
    }

    /**
     * 根据品牌id查询分类集合
     * @param brandId
     * @return
     */
    @GetMapping("/category/of/brand")
    public ResponseEntity<List<CategoryDTO>> findCategoryListByBrandId(@RequestParam(name = "id")Long brandId){
        return ResponseEntity.ok(tbCategoryService.findCategoryListByBrandId(brandId));
    }

    /**
     * 根据cid集合获取分类集合
     * @param cids
     * @return
     */
    @GetMapping("/category/list")
    public ResponseEntity<List<CategoryDTO>> findCategoryListByIds(@RequestParam(name = "ids")List<Long> cids){
        Collection<TbCategory> tbCategories = tbCategoryService.listByIds(cids);
        if(CollectionUtils.isEmpty(tbCategories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        List<TbCategory> tbCategoryList = (List<TbCategory>)tbCategories;
        return ResponseEntity.ok(BeanHelper.copyWithCollection(tbCategoryList,CategoryDTO.class));
    }
}
